package com.todo.event.activity.application.backend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.event.activity.application.backend.service.EventActivityBackendService;
import com.todo.event.activity.application.convert.EventActivityConvert;
import com.todo.event.activity.application.entity.bo.EventActivityBO;
import com.todo.event.activity.application.entity.condition.EventActivityConditionBO;
import com.todo.event.activity.application.service.EventActivityService;
import com.todo.event.activity.common.enums.EventActivityStatusEnum;
import com.todo.event.activity.iface.backend.request.*;
import com.todo.event.activity.iface.backend.response.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2025/1/11 21:28
 * @description
 */
@Slf4j
@Service
public class EventActivityBackendServiceImpl implements EventActivityBackendService {

    @Resource
    private EventActivityService eventActivityService;

    @Override
    public BizResponse<Boolean> insertEventActivity(InsertEventActivityReq req) {
        EventActivityBO eventActivityBO = EventActivityConvert.convertToEventActivityBO(req);
        Boolean inserted = eventActivityService.insertEventActivity(eventActivityBO);
        log.info("EventActivityBackendService#insertEventActivity 新增事件活动成功，入参：【{}】，结果：【{}】", JSONObject.toJSONString(req), inserted);
        return BizResponse.success(inserted);
    }

    @Override
    public BizResponse<Boolean> updateEventActivity(UpdateEventActivityReq req) {
        EventActivityBO eventActivityBO = EventActivityConvert.convertToEventActivityBO(req);
        Boolean updated = eventActivityService.updateEventActivity(eventActivityBO);
        log.info("EventActivityBackendService#updateEventActivity 更新事件活动成功，入参：【{}】，结果：【{}】", JSONObject.toJSONString(req), updated);
        return BizResponse.success(updated);
    }

    @Override
    public BizResponse<Boolean> removeEventActivityById(RemoveEventActivityByIdReq req) {
        EventActivityBO eventActivityBO = eventActivityService.queryEventActivityById(req.getActivityId());
        if (eventActivityBO == null) {
            log.info("EventActivityBackendService#removeEventActivityById 删除活动数据操作完成，活动不存在 eventActivityBO：【{}】", JSONObject.toJSONString(eventActivityBO));
            return BizResponse.success();
        }
        if (!eventActivityBO.getCreateUser().equals(req.get_staff().getUsername())) {
            log.error("EventActivityService#removeEventActivity 删除活动数据操作失败，当前用户无权限操作 eventActivityBO：【{}】", JSONObject.toJSONString(eventActivityBO));
            throw new BizException(BizErrorCodeEnum.ILLEGAL_OPERATION, "当前用户无权限操作");
        }
        Boolean removed = eventActivityService.removeEventActivity(req.getActivityId());
        log.info("EventActivityBackendService#removeEventActivityById 删除事件活动成功，入参：【{}】，结果：【{}】", JSONObject.toJSONString(req), removed);
        return BizResponse.success(removed);
    }

    @Override
    public BizResponse<EventActivityDetailResp> queryEventActivityById(QueryEventActivityByIdReq req) {
        EventActivityBO eventActivityBO = eventActivityService.queryEventActivityById(req.getActivityId());
        log.info("EventActivityBackendService#queryEventActivityById 查询事件活动详情成功，入参：【{}】，结果：【{}】", JSONObject.toJSONString(req), JSONObject.toJSONString(eventActivityBO));
        return BizResponse.success(EventActivityConvert.convertToEventActivityDetailResp(eventActivityBO));
    }

    @Override
    public BizResponse<Boolean> testEventActivityProperty(TestEventActivityPropertyReq req) {
        //todo 后续可以针对活动配置中的各类字段进行验证
        //同时可以验证奖励和触达配置是否满足，例如模板是否已经启用，奖励发放是否可用等
        //目前默认成功，调整状态
        EventActivityBO eventActivityBO = eventActivityService.queryEventActivityById(req.getId());
        if (eventActivityBO == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (!eventActivityBO.getStatus().equals(EventActivityStatusEnum.DRAFT)) {
            log.error("EventActivityBackendService#testEventActivityProperty 活动状态非草稿，无法测试活动属性 入参：【{}】", JSONObject.toJSONString(req));
            throw new BizException(BizErrorCodeEnum.NOT_SUPPORT_OPERATE_STATUS, "当前活动状态不为草稿，无法操作");
        }
        Boolean updated = eventActivityService.updateEventActivityStatus(req.getId(), EventActivityStatusEnum.DRAFT, EventActivityStatusEnum.AUDIT_SUCCESS);
        return BizResponse.success(updated);
    }

    @Override
    public BizResponse<Boolean> enableEventActivity(EnableEventActivityReq req) {
        EventActivityBO eventActivityBO = eventActivityService.queryEventActivityById(req.getId());
        if (eventActivityBO == null) {
            log.error("EventActivityBackendService#enableEventActivity 活动不存在 入参：【{}】", JSONObject.toJSONString(req));
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (req.getEnable() && eventActivityBO.getStatus().equals(EventActivityStatusEnum.RUNNING)) {
            return BizResponse.success(true);
        }
        if (req.getEnable()) {
            if (eventActivityBO.getStatus().equals(EventActivityStatusEnum.DRAFT)) {
                log.error("EventActivityBackendService#enableEventActivity 当前状态不允许操作，入参：【{}】", JSONObject.toJSONString(req));
                throw new BizException(BizErrorCodeEnum.PARAM_ERROR, "当前状态不允许操作");
            }
        }
        if (!req.getEnable() && eventActivityBO.getStatus().equals(EventActivityStatusEnum.SHUTDOWN)) {
            return BizResponse.success(true);
        }
        Boolean updated = eventActivityService.updateEventActivityStatus(req.getId(), eventActivityBO.getStatus(), req.getEnable() ? EventActivityStatusEnum.RUNNING : EventActivityStatusEnum.SHUTDOWN);
        return BizResponse.success(updated);
    }

    @Override
    public BizPageResponse<EventActivityInfoResp> queryEventActivityPage(QueryEventActivityPageReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        EventActivityConditionBO conditionBO = EventActivityConvert.convertToEventActivityConditionBO(req);
        PageResult<EventActivityBO> pageResult = eventActivityService.queryEventActivityPageByCondition(conditionBO);
        return BizPageResponse.success(EventActivityConvert.convertToEventActivityInfoRespPage(pageResult));
    }

    @Override
    public BizResponse<List<EAExecuteRewardRecordResp>> queryEAExecuteRewardRecord(QueryEAExecuteRewardRecordReq req) {
        return null;
    }

    @Override
    public BizResponse<List<EAExecuteTouchRecordResp>> queryEAExecuteTouchRecord(QueryEAExecuteTouchRecordReq req) {
        return null;
    }

    @Override
    public BizResponse<List<EAExecuteFlowPointResp>> queryEAExecuteFlowPoint(QueryEAExecuteFlowPointReq req) {
        return null;
    }

}
